package com.hz.edu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hz.api.client.StudentApi;
import com.hz.api.client.TeacherApi;
import com.hz.api.model.dto.StudentDTO;
import com.hz.api.model.dto.TeacherDTO;
import com.hz.api.model.enums.ApiResponseCodeEnum;
import com.hz.api.model.request.ApiResponse;
import com.hz.edu.mapper.CourseMapper;
import com.hz.edu.mapper.UserCourseMapper;
import com.hz.edu.model.dto.UserSelectCourseDTO;
import com.hz.edu.model.entity.CourseEntity;
import com.hz.edu.model.entity.UserCourseEntity;
import com.hz.edu.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    UserCourseMapper userCourseMapper;

    @Resource
    StudentApi studentApi;

    @Resource
    TeacherApi teacherApi;

    @Resource
    CourseMapper courseMapper;

    @Override
    public int selectCourse(Integer userId, Integer courseId) {
        // 先判断学生有没有选过这门课
        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseAndStudentId(courseId, userId);
        if (userCourseEntity != null) {
            return 0;
        }

        // 课程是否存在
        CourseEntity courseEntity = courseMapper.selectById(courseId);
        if (courseEntity == null) {
            return 0;
        }

        // 获取学生信息
        ApiResponse res = JSON.parseObject(studentApi.studentInfo(userId), ApiResponse.class);
        if (!res.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
            return 0;
        }
        StudentDTO studentDTO = JSON.parseObject(JSON.toJSONString(res.getData()), StudentDTO.class);

        UserCourseEntity newSelect = new UserCourseEntity();
        newSelect.setCourse_id(courseId);
        newSelect.setCourse_name(courseEntity.getCourse_name());
        newSelect.setCourse_code(courseEntity.getCourse_code());
        newSelect.setTeacher_user_id(courseEntity.getTeacher_user_id());
        newSelect.setTeacher_realname(courseEntity.getTeacher_realname());
        newSelect.setStudent_user_id(userId);
        newSelect.setStudent_realname(studentDTO.getRealname());
        newSelect.setUser_score(0);

        return userCourseMapper.addUserCourse(newSelect);
    }

    @Override
    public int cancelCourse(Integer userId, Integer courseId) {
        return userCourseMapper.deleteUserCourse(courseId, userId);
    }

    @Override
    public List<UserSelectCourseDTO> selectedCourses(Integer studentId) {
        List<UserCourseEntity> userCourses = userCourseMapper.selectedCoursesOfStudentId(studentId);
        List<UserSelectCourseDTO> myCourses = new ArrayList<>();
        userCourses.forEach(userCourseEntity -> {
            UserSelectCourseDTO dto = new UserSelectCourseDTO();
            dto.setCourseId(userCourseEntity.getCourse_id());
            dto.setCourseCode(userCourseEntity.getCourse_code());
            dto.setCourseName(userCourseEntity.getCourse_code());
            dto.setTeacherRealname(userCourseEntity.getTeacher_realname());
            CourseEntity courseEntity = courseMapper.selectById(userCourseEntity.getCourse_id());
            dto.setSectionCount(courseEntity.getSection_count());
            dto.setCourseTime(courseEntity.getCourse_time());
            dto.setCourseProfile(courseEntity.getCourse_profile());
            dto.setTextbook(courseEntity.getTextbook());
            dto.setCreditsCount(courseEntity.getCredits_count());
            dto.setAllScore(courseEntity.getAll_score());
            dto.setPassScore(courseEntity.getPass_score());
            dto.setMyScore(userCourseEntity.getUser_score());
            myCourses.add(dto);
        });
        return myCourses;
    }

    @Override
    public UserSelectCourseDTO selectedCourseInfo(Integer studentId, Integer courseId) {
        CourseEntity courseEntity = courseMapper.selectById(courseId);
        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseAndStudentId(courseId, studentId);
        UserSelectCourseDTO dto = new UserSelectCourseDTO();
        dto.setCourseId(userCourseEntity.getCourse_id());
        dto.setCourseCode(userCourseEntity.getCourse_code());
        dto.setCourseName(userCourseEntity.getCourse_code());
        dto.setTeacherRealname(userCourseEntity.getTeacher_realname());
        dto.setSectionCount(courseEntity.getSection_count());
        dto.setCourseTime(courseEntity.getCourse_time());
        dto.setCourseProfile(courseEntity.getCourse_profile());
        dto.setTextbook(courseEntity.getTextbook());
        dto.setCreditsCount(courseEntity.getCredits_count());
        dto.setAllScore(courseEntity.getAll_score());
        dto.setPassScore(courseEntity.getPass_score());
        dto.setMyScore(userCourseEntity.getUser_score());
        return dto;
    }
}
